package analysis;

import analysis.entity.AminoAcid;

import java.io.*;
import java.net.URL;
import java.util.*;

/**
 * 〈主类-菌族差异统计〉
 *
 * @author shiweixian
 * @date 2018/3/8
 * @since 1.0.0
 */
public class Main {

    private static void print(String s) {
        System.out.print(s + "  ");
    }

    private static void println(String s) {
        System.out.println(s);
    }

    public static String format(double value) {
        return new Formatter().format("%.2f", value).toString();
    }

    public static Double format2Double(double value) {
        return Double.parseDouble(new Formatter().format("%.2f", value).toString());
    }

    public static void main(String[] args) {
        Main main = new Main();
        URL url = main.getClass().getClassLoader().getResource("TP.fas");
        String filePath = url.getPath();
        try {
            List<HashMap<Character, Integer>> list = new ArrayList<HashMap<Character, Integer>>();
            BufferedReader reader = new BufferedReader(new FileReader(new File(filePath)));
            String temp;
            int count = 0;
            int rows = 0;
            while ((temp = reader.readLine()) != null) {
                count++;
                if (count % 2 != 0) {
                    continue;
                }

                println("第" + count / 2 + "行数据：");
                println(temp);

                if (count / 2 == 1) {
                    //读取第一行数据时，根据数据长度初始化list
                    for (int i = 0; i < temp.length(); i++) {
                        list.add(new HashMap<Character, Integer>());
                    }
                }
                // 只要偶数行的数据
                for (int i = 0; i < temp.length(); i++) {
                    char c = temp.charAt(i);
                    HashMap<Character, Integer> map = list.get(i);
                    if (map.containsKey(c)) {
                        map.put(c, map.get(c) + 1);
                    } else {
                        map.put(c, 1);
                    }
                }

                rows++;
            }

            //保存统计结果
            List<List<AminoAcid>> result = new ArrayList<List<AminoAcid>>();
            for (int i = 0; i < list.size(); i++) {
                List<AminoAcid> acids = new ArrayList<AminoAcid>();
                HashMap<Character, Integer> map = list.get(i);
                if (map.keySet().size() == 1) {
                    AminoAcid acid = new AminoAcid();
                    acid.setName(map.keySet().toArray(new Character[0])[0]);
                    acid.setRatio(100);
                    acids.add(acid);
                    result.add(acids);
                } else {
                    for (Character c : map.keySet()) {
                        AminoAcid acid = new AminoAcid();
                        acid.setName(c);
                        acid.setRatio(format2Double(map.get(c) * 100.0 / rows));
                        acids.add(acid);
                    }
                    Collections.sort(acids);
                }
                result.add(acids);
            }

            BufferedWriter writer = new BufferedWriter(new FileWriter("D:/result.txt"));
            for (int i = 0; i < result.size(); i++) {
                println("第" + (i + 1) + "列：");
                writer.write("第" + (i + 1) + "列：\n");
                for (AminoAcid acid : result.get(i)) {
                    print(acid.getName() + ":" + acid.getRatio() + "%");
                    writer.write(acid.getName() + ":" + acid.getRatio() + "% ");
                }
                println("");
                writer.write("\n");
            }
            writer.flush();
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}